﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using AccessControl.Business;
using AccessControl.Common.Enumerations;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;


namespace AccessControl.SecurityMonitoring.Form
{
    public partial class AddPerson : XtraForm
    {
        #region Field

        #endregion

        #region Singletone Members

        private static AddPerson _addPerson;

        private AddPerson()
        {
            InitializeComponent();

            //var dataTable = new DataTable();
            //var keyDataColumn = new DataColumn("Key", typeof(byte));
            //var valueDataColumn = new DataColumn("Value", typeof(byte));
            //dataTable.Columns.Add(keyDataColumn);
            //dataTable.Columns.Add(valueDataColumn);
            //var row1 = dataTable.NewRow();
            //row1["Key"] = PersonTitleConstants.Student;
            //row1["Value"] = Enum;

            var a = Utility.EnumHelper.EnumHelper.ToList(typeof(PersonTitleConstants));
            gridLookUpEditPesonTitle.Properties.DataSource = a;

        }

        public static AddPerson GetInstance()
        {
            if ((_addPerson == null) || (_addPerson.IsDisposed))
                _addPerson = new AddPerson();
            return _addPerson;
        }

        #endregion

        #region Event Handler

        private void pictureEditPersonPhoto_Click(object sender, System.EventArgs e)
        {
            var result = openFileDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
                pictureEditPersonPhoto.Image = Image.FromFile(openFileDialog.FileName);

        }

        private void simpleButtonSubmit_Click(object sender, System.EventArgs e)
        {
            AddPersonToDatabse();

        }

        private void simpleButtonClose_Click(object sender, System.EventArgs e)
        {
            Close();
        }

        #endregion

        #region Private Method

        private bool CheckAllRequeredField()
        {
            errorProvider.Clear();
            var hasError = true;
            if (textEditPkPerson.Text == string.Empty)
            {
                errorProvider.SetError(textEditPkPerson, "شناسه فرد را وارد نمایید");
                hasError = false;
            }

            if (textEditPersonFirstName.Text == string.Empty)
            {
                errorProvider.SetError(textEditPersonFirstName, "نام فرد را وارد نمایید");
                hasError = false;
            }

            if (textEditPersonLastName.Text == string.Empty)
            {
                errorProvider.SetError(textEditPersonLastName, "نام خانوادگی فرد را وارد نمایید");
                hasError = false;
            }

            if (textEditPersonRefNumber.Text == string.Empty)
            {
                errorProvider.SetError(textEditPersonRefNumber, "شماره شناسایی فرد را وارد نمایید");
                hasError = false;
            }
            if (gridLookUpEditPesonTitle.EditValue==null)
            {
                errorProvider.SetError(gridLookUpEditPesonTitle, "عنوان فرد را وارد نمایید");
                hasError = false;
            }

            return hasError;
        }

        private void AddPersonToDatabse()
        {
            if (!CheckAllRequeredField())
                return;
            try
            {
                Common.AccessControlDS.tblPersonRow row = new Common.AccessControlDS.tblPersonDataTable().NewtblPersonRow();
                row.pkPerson = textEditPkPerson.Text;
                row.PersonInfoDate = Util.GetServerDateTime();
                row.PersonFirstName = textEditPersonFirstName.Text;
                row.PersonLastName = textEditPersonLastName.Text;
                row.PersonIsMale = (bool) radioGroupPersonIsMale.EditValue;
                row.PersonTitle = (Byte)((PersonTitleConstants)gridLookUpEditPesonTitle.EditValue);
                row.PersonDesc = memoEditPersonDesc.Text;
                if (faDatePickerPersonExpireDate.SelectedDateTime.HasValue)
                    row.PersonExpireDate = faDatePickerPersonExpireDate.SelectedDateTime.Value;
        
                row.PersonRefNumber = textEditPersonRefNumber.Text;
                if (pictureEditPersonPhoto.Image != null)
                row.PersonPhoto   = ByteImageConverter.ToByteArray(pictureEditPersonPhoto.Image, ImageFormat.Jpeg);
           
                
                PersonBusiness.Insert(row);

                Close();


            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("خطا در به ثبت فرد\n" + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        #endregion

    }
}